package lxt.action;

import java.util.List;

import lxt.action.core.ActionSupport;
import lxt.entity.ClientInfo;
import lxt.service.ClientService;

/**
 * 客户管理actio
 * @author Lit
 *
 */
public class ClienteleAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	//定义接口
	private ClientService clientService;
	//定义变量
	private List<ClientInfo> clients;
	private String clientId;
	private String linkman;
	private String phone;
	private String clientName;
	private String address;
	
	private String mark;
	
	private String deleteIds;
	//定义分页变量
	private Integer pageCounts = 10; //每页显示数量
	private String currPageStr = "1"; //分页
	private Integer totalCounts ;//总数据量
	
	/**
	 * 
	 * 查询客户列表
	 */
	public String loadAll(){
		try{
			//定义从第几条显示
			int offset =( Integer.parseInt(currPageStr)-1)*pageCounts;
			//获得list
			clients = clientService.loadClient(linkman,address,clientName,offset,pageCounts);
			//获得总共数据量
			totalCounts = clientService.loadAllClientCount(linkman, address, clientName);
			
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage("操作失败，请检查重新操作");
		}
		return SUCCESS;
	}
	/**
	 * 添加客户预处理
	 * @return
	 */
	public String AddPre(){
		return SUCCESS;
	}
	/**
	 * 添加客户
	 * @return
	 */
	public String add(){
		try{
			//定义变量 client
			ClientInfo client = new ClientInfo();
			//为client赋值
			client.setClientName(clientName);
			client.setLinkman(linkman);
			client.setPhone(phone);
			client.setAddress(address);
			
			this.clientService.addClient(client);
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage("操作失败，请检查重新操作");
		}
		return SUCCESS;
	}
	/**
	 * 编辑客户预处理
	 * @return
	 */
	public String updatePre(){
		try{
			ClientInfo client = this.clientService.loadClientById(clientId);
			this.clientId = client.getClientId();
			this.clientName = client.getClientName();
			this.address = client.getAddress();
			this.linkman = client.getLinkman();
			this.phone = client.getPhone();
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage("操作失败，请检查重新操作");
		}
		return SUCCESS;
	}
	/**
	 * 修改客户
	 * @return
	 */
	public String update(){
		try{
			//重新获取修改目标
			ClientInfo client = this.clientService.loadClientById(clientId);
			//更新目标对象的目标修改属性(部分属性)
			client.setClientName(clientName);
			client.setLinkman(linkman);
			client.setPhone(phone);
			client.setAddress(address);
			
			clientService.updateClient(client);
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage("操作失败，请检查重新操作");
		}
		return SUCCESS;
	}
	/**
	 * 删除客户
	 * @return
	 */
	public String delete(){
		try{
			clientService.deleteClient(deleteIds);
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage(ex.getMessage());
			this.loadAll();
			return INPUT;
		}
		return SUCCESS;
	}
	
	
	public void setClientService(ClientService clientService) {
		this.clientService = clientService;
	}
	public List<ClientInfo> getClients() {
		return clients;
	}
	public void setClients(List<ClientInfo> clients) {
		this.clients = clients;
	}
	public String getClientId() {
		return clientId;
	}
	public void setClientId(String clientId) {
		this.clientId = clientId;
	}
	public String getLinkman() {
		return linkman;
	}
	public void setLinkman(String linkman) {
		this.linkman = linkman;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getClientName() {
		return clientName;
	}
	public void setClientName(String clientName) {
		this.clientName = clientName;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public Integer getPageCounts() {
		return pageCounts;
	}
	public void setPageCounts(Integer pageCounts) {
		this.pageCounts = pageCounts;
	}
	public String getCurrPageStr() {
		return currPageStr;
	}
	public void setCurrPageStr(String currPageStr) {
		System.out.println("值为："+ currPageStr);
		this.currPageStr = currPageStr;
	}
	public Integer getTotalCounts() {
		return totalCounts;
	}
	public void setTotalCounts(Integer totalCounts) {
		this.totalCounts = totalCounts;
	}
	public String getDeleteIds() {
		return deleteIds;
	}
	public void setDeleteIds(String deleteIds) {
		this.deleteIds = deleteIds;
	}
	public String getMark() {
		return mark;
	}
	public void setMark(String mark) {
		this.mark = mark;
	}

}
